/**
 * All rights Reserved, Designed By 信鸥科技
 * project : committee
 * Created by youquer on 17/10/31.
 * 注意：本内容仅限于信鸥科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 * Description:
 *
 */

import { postChangePwd } from '../services/others';
import { hex_md5 } from '@/utils/md5';
import { message } from 'antd';


export default {

  namespace: 'pwd',

  state: {
    type: "pwd",
    newPwd: "",
    oldPwd: "",
    loading: false,
  },

  reducers: {
    onCollapse(state, action) {
      return { ...state, mode: action.mode, collapsed: action.collapsed };
    },
    /**
     * 赋值数据
     * */
    setState(state, action){
      return {...state, ...action.payload};
    },
    //提交按钮
    submit(state,action){
      return {...state,loading:action.loading}
    },
    //添加页获取数据
    changeState(state, action){
      state[action.key] = action.value;
      return {...state, state};
    },
  },

  effects: {
    //列表页,分页,搜索页
    *changePwd({payload}, {call, put, select}){
      var params = {
        newPwd: hex_md5(payload.newPwd),
        oldPwd: hex_md5(payload.oldPwd),
      };
      const data = yield call(postChangePwd, params);
      if (data) {
        yield put({
          type: "setState",
          payload: {newPwd: "", oldPwd: ""},
        });
        if (data.recode == 200) {
          message.success("修改密码成功");
          window.location.hash = '/user/login';
        } else if (data.recode == 100){
          // message.error("旧密码与当前密码不符");
        } else {
          // message.error("修改密码失败!");
        }
      } else {
        // message.error("修改密码失败!");
      }
    },
  },
};
